<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"llmysnow.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Canvas画圆、时钟、绘图板">
<meta property="og:type" content="article">
<meta property="og:title" content="Canvas 学习（时钟 绘图板）">
<meta property="og:url" content="http://llmysnow.top/posts/453bf97e/index.html">
<meta property="og:site_name" content="奇幻的梦之旅">
<meta property="og:description" content="Canvas画圆、时钟、绘图板">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/%E7%BB%98%E5%9B%BE%E6%9D%BF.gif">
<meta property="article:published_time" content="2021-05-23T05:43:16.145Z">
<meta property="article:modified_time" content="2021-05-26T01:29:13.543Z">
<meta property="article:author" content="LiLyn">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="Canvas">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lilyn/pic/raw/master/company-img/%E7%BB%98%E5%9B%BE%E6%9D%BF.gif">

<link rel="canonical" href="http://llmysnow.top/posts/453bf97e/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Canvas 学习（时钟 绘图板） | 奇幻的梦之旅</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="奇幻的梦之旅" type="application/atom+xml">
</head>
<!--即时预加载技术，在用户点击之前预先加载页面-->
<script src="https://cdn.jsdelivr.net/gh/TRHX/CDN-for-itrhx.com@2.0.2/js/instantclick-1.2.2.js" type="module"></script>
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	  <!--<a target="_blank" rel="noopener" href="https://github.com/llwodexue" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>-->
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">奇幻的梦之旅</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://llmysnow.top/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://llmysnow.top/posts/453bf97e/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/logo.jpg">
      <meta itemprop="name" content="LiLyn">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇幻的梦之旅">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Canvas 学习（时钟 绘图板）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-23 13:43:16" itemprop="dateCreated datePublished" datetime="2021-05-23T13:43:16+08:00">2021-05-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>6.3k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>
            <div class="post-description">Canvas画圆、时钟、绘图板</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="canvas画圆"><a href="#canvas画圆" class="headerlink" title="canvas画圆"></a>canvas画圆</h2><ul>
<li>为什么需要用canvas，如果用<code>onmousemove</code>，每一次移动都会进行DOM重绘，非常消耗性能，这时我们需要看一下 canvas</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;canvas&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    canvas.onmousemove = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">let</span> div = <span class="built_in">document</span>.createElement(<span class="string">&quot;div&quot;</span>);</span></span><br><span class="line"><span class="javascript">        div.style.position = <span class="string">&quot;absolute&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.left = e.clientX + <span class="string">&quot;px&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.top = e.clientY + <span class="string">&quot;px&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.width = <span class="string">&quot;6px&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.height = <span class="string">&quot;6px&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.marginLeft = <span class="string">&quot;-3px&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.marginRight = <span class="string">&quot;-3px&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.borderRadius = <span class="string">&quot;50%&quot;</span>;</span></span><br><span class="line"><span class="javascript">        div.style.backgroundColor = <span class="string">&quot;black&quot;</span>;</span></span><br><span class="line">        canvas.appendChild(div);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>我们现在已经给 canvas 设置一屏宽高，但还是出现滚动条，原因是 canvas 是 inline 元素，设置宽高不起作用，所以需要把它转换成块级元素（<strong>不要这样做，这样做会拉伸元素</strong>）</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;canvas&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="css">    <span class="selector-id">#canvas</span> &#123;</span></span><br><span class="line">        height: 100vh;</span><br><span class="line">        width: 100vw;</span><br><span class="line">        border: 1px solid red;</span><br><span class="line"><span class="css">        <span class="comment">/* display: block; */</span></span></span><br><span class="line">    &#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li><code>&lt;canvas&gt;</code> 标签只有两个属性<strong>——</strong> <code>width</code>和<code>height</code>，可以使用 JS 设置canvas 宽高，这里不能直接给 canvas 设置 <code>height: 100vh;</code> <code>width: 100vw;</code> </li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">&quot;canvas&quot;</span>);</span><br><span class="line">canvas.width = <span class="built_in">document</span>.documentElement.clientWidth;</span><br><span class="line">canvas.height = <span class="built_in">document</span>.documentElement.clientHeight;</span><br></pre></td></tr></table></figure>

<ul>
<li>canvas画圆</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> painting = <span class="literal">false</span>;</span><br><span class="line"><span class="keyword">let</span> ctx = canvas.getContext(<span class="string">&quot;2d&quot;</span>);</span><br><span class="line">ctx.fillStyle = <span class="string">&quot;black&quot;</span>;</span><br><span class="line">ctx.strokeStyle = <span class="string">&quot;none&quot;</span>;</span><br><span class="line"></span><br><span class="line">canvas.onmousedown = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">    painting = <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line">canvas.onmousemove = <span class="function">(<span class="params">e</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (painting === <span class="literal">true</span>) &#123;</span><br><span class="line">        ctx.beginPath();</span><br><span class="line">        ctx.arc(e.clientX, e.clientY, <span class="number">10</span>, <span class="number">0</span>, <span class="number">2</span> * <span class="built_in">Math</span>.PI);</span><br><span class="line">        ctx.stroke();</span><br><span class="line">        ctx.fill()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">canvas.onmouseup = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    painting = <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Canvas"><a href="#Canvas" class="headerlink" title="Canvas"></a>Canvas</h2><h3 id="颜色、样式和阴影"><a href="#颜色、样式和阴影" class="headerlink" title="颜色、样式和阴影"></a>颜色、样式和阴影</h3><table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_fillstyle.asp">fillStyle</a></td>
<td>设置或返回用于填充绘画的颜色、渐变或模式</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_strokestyle.asp">strokeStyle</a></td>
<td>设置或返回用于笔触的颜色、渐变或模式</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_shadowcolor.asp">shadowColor</a></td>
<td>设置或返回用于阴影的颜色</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_shadowblur.asp">shadowBlur</a></td>
<td>设置或返回用于阴影的模糊级别</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_shadowoffsetx.asp">shadowOffsetX</a></td>
<td>设置或返回阴影距形状的水平距离</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_shadowoffsety.asp">shadowOffsetY</a></td>
<td>设置或返回阴影距形状的垂直距离</td>
</tr>
</tbody></table>
<h3 id="线条样式"><a href="#线条样式" class="headerlink" title="线条样式"></a>线条样式</h3><table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_linecap.asp">lineCap</a></td>
<td>设置或返回线条的结束端点样式</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_linejoin.asp">lineJoin</a></td>
<td>设置或返回两条线相交时，所创建的拐角类型</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_linewidth.asp">lineWidth</a></td>
<td>设置或返回当前的线条宽度</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_miterlimit.asp">miterLimit</a></td>
<td>设置或返回最大斜接长度</td>
</tr>
</tbody></table>
<h3 id="矩形"><a href="#矩形" class="headerlink" title="矩形"></a>矩形</h3><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_rect.asp">rect()</a></td>
<td>创建矩形</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_fillrect.asp">fillRect()</a></td>
<td>绘制“被填充”的矩形</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_strokerect.asp">strokeRect()</a></td>
<td>绘制矩形（无填充）</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_clearrect.asp">clearRect()</a></td>
<td>在给定的矩形内清除指定的像素</td>
</tr>
</tbody></table>
<h3 id="路径"><a href="#路径" class="headerlink" title="路径"></a>路径</h3><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_fill.asp">fill()</a></td>
<td>填充当前绘图（路径）</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_stroke.asp">stroke()</a></td>
<td>绘制已定义的路径</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_beginpath.asp">beginPath()</a></td>
<td>起始一条路径，或重置当前路径</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_moveto.asp">moveTo()</a></td>
<td>把路径移动到画布中的指定点，不创建线条</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_closepath.asp">closePath()</a></td>
<td>创建从当前点回到起始点的路径</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_lineto.asp">lineTo()</a></td>
<td>添加一个新点，然后在画布中创建从该点到最后指定点的线条</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_clip.asp">clip()</a></td>
<td>从原始画布剪切任意形状和尺寸的区域</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_quadraticcurveto.asp">quadraticCurveTo()</a></td>
<td>创建二次贝塞尔曲线</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_beziercurveto.asp">bezierCurveTo()</a></td>
<td>创建三次方贝塞尔曲线</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_arc.asp">arc()</a></td>
<td>创建弧/曲线（用于创建圆形或部分圆）</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_arcto.asp">arcTo()</a></td>
<td>创建两切线之间的弧/曲线</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_ispointinpath.asp">isPointInPath()</a></td>
<td>如果指定的点位于当前路径中，则返回 true，否则返回 false</td>
</tr>
</tbody></table>
<h3 id="转换"><a href="#转换" class="headerlink" title="转换"></a>转换</h3><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_scale.asp">scale()</a></td>
<td>缩放当前绘图至更大或更小</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_rotate.asp">rotate()</a></td>
<td>旋转当前绘图</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_translate.asp">translate()</a></td>
<td>重新映射画布上的 (0,0) 位置</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_transform.asp">transform()</a></td>
<td>替换绘图的当前转换矩阵</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_settransform.asp">setTransform()</a></td>
<td>将当前转换重置为单位矩阵。然后运行 transform()</td>
</tr>
</tbody></table>
<h3 id="文本"><a href="#文本" class="headerlink" title="文本"></a>文本</h3><table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_font.asp">font</a></td>
<td>设置或返回文本内容的当前字体属性</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_textalign.asp">textAlign</a></td>
<td>设置或返回文本内容的当前对齐方式</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_textbaseline.asp">textBaseline</a></td>
<td>设置或返回在绘制文本时使用的当前文本基线</td>
</tr>
</tbody></table>
<h3 id="图像绘制"><a href="#图像绘制" class="headerlink" title="图像绘制"></a>图像绘制</h3><table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/canvas_drawimage.asp">drawImage()</a></td>
<td>向画布上绘制图像、画布或视频</td>
</tr>
</tbody></table>
<h3 id="像素操作"><a href="#像素操作" class="headerlink" title="像素操作"></a>像素操作</h3><table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>width</td>
<td>返回 ImageData 对象的宽度</td>
</tr>
<tr>
<td>height</td>
<td>返回 ImageData 对象的高度</td>
</tr>
<tr>
<td>data</td>
<td>返回一个对象，其包含指定的 ImageData 对象的图像数据</td>
</tr>
</tbody></table>
<h2 id="Canvas练习"><a href="#Canvas练习" class="headerlink" title="Canvas练习"></a>Canvas练习</h2><h3 id="绘制圆形和文本"><a href="#绘制圆形和文本" class="headerlink" title="绘制圆形和文本"></a>绘制圆形和文本</h3><ul>
<li>绘制线段</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">ctx.beginPath();</span><br><span class="line"><span class="comment">// 设置绘制的起始点</span></span><br><span class="line">ctx.moveTo(<span class="number">50</span>, <span class="number">50</span>);</span><br><span class="line"><span class="comment">// 设置经过某个位置</span></span><br><span class="line">ctx.lineTo(<span class="number">50</span>, <span class="number">300</span>);</span><br><span class="line">ctx.lineTo(<span class="number">300</span>, <span class="number">100</span>);</span><br><span class="line"><span class="comment">// 设置绘制的结束点</span></span><br><span class="line">ctx.closePath();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 起始路径的线段边缘设置为圆角</span></span><br><span class="line">ctx.lineCap = <span class="string">&quot;round&quot;</span>;</span><br><span class="line"><span class="comment">// 转折处的线段设置为圆角</span></span><br><span class="line">ctx.lineJoin = <span class="string">&quot;round&quot;</span></span><br><span class="line"><span class="comment">// 设置颜色样式</span></span><br><span class="line">ctx.strokeStyle = <span class="string">&quot;aqua&quot;</span>;</span><br><span class="line">ctx.lineWidth = <span class="number">20</span>;</span><br><span class="line">ctx.stroke()</span><br></pre></td></tr></table></figure>

<ul>
<li>绘制圆</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 默认为false，顺时针，true逆时针</span></span><br><span class="line"><span class="comment">// ctx,arc(x, y, radius, startAngle, endAngle, anticlockwise)</span></span><br><span class="line">ctx.arc(<span class="number">300</span>, <span class="number">300</span>, <span class="number">100</span>, <span class="number">0</span>, <span class="number">2</span> * <span class="built_in">Math</span>.PI, <span class="literal">true</span>);</span><br><span class="line">ctx.fillStyle = <span class="string">&quot;bisque&quot;</span>;</span><br><span class="line">ctx.fill();</span><br><span class="line">ctx.stroke();</span><br></pre></td></tr></table></figure>

<ul>
<li>弹幕字体</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">ctx.font = <span class="string">&quot;50px sans-serif&quot;</span></span><br><span class="line"><span class="keyword">var</span> x = <span class="number">1200</span>;</span><br><span class="line"><span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    ctx.clearRect(<span class="number">0</span>, <span class="number">0</span>, <span class="number">1200</span>, <span class="number">1200</span>);</span><br><span class="line">    x -= <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">if</span> (x &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">        x = <span class="number">1200</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    ctx.fillText(<span class="string">&quot;hello&quot;</span>, x, <span class="number">100</span>);</span><br><span class="line">    ctx.strokeText(<span class="string">&quot;world&quot;</span>, x, <span class="number">200</span>);</span><br><span class="line">&#125;, <span class="number">40</span>)</span><br></pre></td></tr></table></figure>

<h3 id="绘制图形"><a href="#绘制图形" class="headerlink" title="绘制图形"></a>绘制图形</h3><ul>
<li>绘制图像（同一图片）</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> img = <span class="keyword">new</span> Image();</span><br><span class="line">img.src = <span class="string">&quot;./imgs/2.jpg&quot;</span>;</span><br><span class="line">img.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    ctx.drawImage(img, <span class="number">50</span>, <span class="number">100</span>, <span class="number">480</span>, <span class="number">270</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> img2 = <span class="keyword">new</span> Image();</span><br><span class="line">img2.src = <span class="string">&quot;./imgs/3.jpg&quot;</span>;</span><br><span class="line">img2.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    ctx.drawImage(img2, <span class="number">400</span>, <span class="number">400</span>, <span class="number">480</span>, <span class="number">270</span>, <span class="number">100</span>, <span class="number">400</span>, <span class="number">480</span>, <span class="number">270</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>绘制图像（视频抽帧）</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> video = <span class="built_in">document</span>.querySelector(<span class="string">&quot;video&quot;</span>);</span><br><span class="line"><span class="keyword">var</span> interval = <span class="literal">null</span>;</span><br><span class="line">video.onplay = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    interval = <span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        ctx.drawImage(video, <span class="number">0</span>, <span class="number">800</span>, <span class="number">800</span>, <span class="number">400</span>)</span><br><span class="line">    &#125;, <span class="number">32</span>)</span><br><span class="line">&#125;</span><br><span class="line">video.onpause = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">clearInterval</span>(interval);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="绘制时钟"><a href="#绘制时钟" class="headerlink" title="绘制时钟"></a>绘制时钟</h2><p>ES6版本参考这个（附带效果）：<a target="_blank" rel="noopener" href="https://github.com/llwodexue/clock">https://github.com/llwodexue/clock</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> canvas1 = <span class="built_in">document</span>.querySelector(<span class="string">&quot;#canvas1&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> ctx = canvas1.getContext(<span class="string">&quot;2d&quot;</span>);</span><br><span class="line">ctx.clearRect(<span class="number">0</span>, <span class="number">0</span>, <span class="number">800</span>, <span class="number">600</span>);</span><br><span class="line">ctx.save();</span><br><span class="line">ctx.translate(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">ctx.rotate(-<span class="built_in">Math</span>.PI / <span class="number">2</span>);</span><br><span class="line"><span class="comment">// 绘制表盘</span></span><br><span class="line">ctx.beginPath();</span><br><span class="line">ctx.arc(<span class="number">0</span>, <span class="number">0</span>, <span class="number">200</span>, <span class="number">0</span>, <span class="number">2</span> * <span class="built_in">Math</span>.PI);</span><br><span class="line">ctx.strokeStyle = <span class="string">&quot;darkgrey&quot;</span>;</span><br><span class="line">ctx.lineWidth = <span class="number">10</span>;</span><br><span class="line">ctx.stroke();</span><br><span class="line">ctx.closePath();</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">renderClock</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    ctx.clearRect(<span class="number">0</span>, <span class="number">0</span>, <span class="number">800</span>, <span class="number">600</span>);</span><br><span class="line">    ctx.save();</span><br><span class="line">    ctx.translate(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">    ctx.rotate(-<span class="built_in">Math</span>.PI / <span class="number">2</span>);</span><br><span class="line">    <span class="comment">// 绘制表盘</span></span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.arc(<span class="number">0</span>, <span class="number">0</span>, <span class="number">200</span>, <span class="number">0</span>, <span class="number">2</span> * <span class="built_in">Math</span>.PI);</span><br><span class="line">    ctx.strokeStyle = <span class="string">&quot;darkgrey&quot;</span>;</span><br><span class="line">    ctx.lineWidth = <span class="number">10</span>;</span><br><span class="line">    ctx.stroke();</span><br><span class="line">    ctx.closePath();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">drawLine</span>(<span class="params">&#123; count, start, end, width, color &#125;</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">let</span> i = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (; i &lt; count; i++) &#123;</span><br><span class="line">            ctx.beginPath();</span><br><span class="line">            ctx.rotate((<span class="number">2</span> * <span class="built_in">Math</span>.PI) / count);</span><br><span class="line">            ctx.moveTo(start, <span class="number">0</span>);</span><br><span class="line">            ctx.lineTo(end, <span class="number">0</span>);</span><br><span class="line">            ctx.lineWidth = width;</span><br><span class="line">            ctx.strokeStyle = color;</span><br><span class="line">            ctx.stroke();</span><br><span class="line">            ctx.closePath();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 绘制分针刻度线</span></span><br><span class="line">    drawLine(&#123;</span><br><span class="line">        count: <span class="number">60</span>,</span><br><span class="line">        start: <span class="number">180</span>,</span><br><span class="line">        end: <span class="number">190</span>,</span><br><span class="line">        width: <span class="number">2</span>,</span><br><span class="line">        color: <span class="string">&quot;orangered&quot;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// 绘制时针刻度线</span></span><br><span class="line">    drawLine(&#123;</span><br><span class="line">        count: <span class="number">12</span>,</span><br><span class="line">        start: <span class="number">180</span>,</span><br><span class="line">        end: <span class="number">200</span>,</span><br><span class="line">        width: <span class="number">10</span>,</span><br><span class="line">        color: <span class="string">&quot;darkgrey&quot;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> time = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">    <span class="keyword">let</span> min = time.getMinutes();</span><br><span class="line">    <span class="keyword">let</span> sec = time.getSeconds();</span><br><span class="line">    <span class="keyword">let</span> hour = time.getHours();</span><br><span class="line">    hour = hour &gt; <span class="number">12</span> ? hour - <span class="number">12</span> : hour;</span><br><span class="line">    <span class="comment">// 绘制秒针</span></span><br><span class="line">    ctx.save();</span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.rotate(((<span class="number">2</span> * <span class="built_in">Math</span>.PI) / <span class="number">60</span>) * sec);</span><br><span class="line">    ctx.moveTo(-<span class="number">30</span>, <span class="number">0</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">170</span>, <span class="number">0</span>);</span><br><span class="line">    ctx.lineWidth = <span class="number">2</span>;</span><br><span class="line">    ctx.strokeStyle = <span class="string">&quot;red&quot;</span>;</span><br><span class="line">    ctx.stroke();</span><br><span class="line">    ctx.closePath();</span><br><span class="line">    ctx.restore();</span><br><span class="line">    <span class="comment">// 绘制分针</span></span><br><span class="line">    ctx.save();</span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.rotate(((<span class="number">2</span> * <span class="built_in">Math</span>.PI) / <span class="number">60</span>) * min + ((<span class="number">2</span> * <span class="built_in">Math</span>.PI) / <span class="number">3600</span>) * sec);</span><br><span class="line">    ctx.moveTo(-<span class="number">20</span>, <span class="number">0</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">150</span>, <span class="number">0</span>);</span><br><span class="line">    ctx.lineWidth = <span class="number">4</span>;</span><br><span class="line">    ctx.strokeStyle = <span class="string">&quot;darkblue&quot;</span>;</span><br><span class="line">    ctx.stroke();</span><br><span class="line">    ctx.closePath();</span><br><span class="line">    ctx.restore();</span><br><span class="line">    <span class="comment">// 绘制时针</span></span><br><span class="line">    ctx.save();</span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.rotate(</span><br><span class="line">        ((<span class="number">2</span> * <span class="built_in">Math</span>.PI) / <span class="number">12</span>) * hour +</span><br><span class="line">            ((<span class="number">2</span> * <span class="built_in">Math</span>.PI) / <span class="number">12</span> / <span class="number">60</span>) * min +</span><br><span class="line">            ((<span class="number">2</span> * <span class="built_in">Math</span>.PI) / <span class="number">12</span> / <span class="number">60</span> / <span class="number">60</span>) * sec</span><br><span class="line">    );</span><br><span class="line">    ctx.moveTo(-<span class="number">10</span>, <span class="number">0</span>);</span><br><span class="line">    ctx.lineTo(<span class="number">130</span>, <span class="number">0</span>);</span><br><span class="line">    ctx.lineWidth = <span class="number">6</span>;</span><br><span class="line">    ctx.strokeStyle = <span class="string">&quot;darkgrey&quot;</span>;</span><br><span class="line">    ctx.stroke();</span><br><span class="line">    ctx.closePath();</span><br><span class="line">    ctx.restore();</span><br><span class="line">    <span class="comment">// 绘制表圈</span></span><br><span class="line">    ctx.beginPath();</span><br><span class="line">    ctx.arc(<span class="number">0</span>, <span class="number">0</span>, <span class="number">10</span>, <span class="number">0</span>, <span class="number">2</span> * <span class="built_in">Math</span>.PI);</span><br><span class="line">    ctx.fillStyle = <span class="string">&quot;deepskyblue&quot;</span>;</span><br><span class="line">    ctx.fill();</span><br><span class="line">    ctx.closePath();</span><br><span class="line">    ctx.restore();</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">setInterval</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    renderClock();</span><br><span class="line">&#125;, <span class="number">1000</span>);</span><br></pre></td></tr></table></figure>

<h2 id="Canvas-绘图板"><a href="#Canvas-绘图板" class="headerlink" title="Canvas 绘图板"></a>Canvas 绘图板</h2><p>绘图板参考（附带效果）： <a target="_blank" rel="noopener" href="https://github.com/llwodexue/canvas_painting">https://github.com/llwodexue/canvas_painting</a></p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/company-img/%E7%BB%98%E5%9B%BE%E6%9D%BF.gif"></p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/266b4fa2c446">Fira Code | 为写程序而生的字体</a></p>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API/Tutorial">Canvas教程</a></p>
<p><a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/3974827/detecting-touch-screen-devices-with-javascript">Detecting touch screen devices with Javascript</a></p>
<p><a target="_blank" rel="noopener" href="https://www.w3school.com.cn/tags/html_ref_canvas.asp">HTML 5 Canvas 参考手册</a></p>

    </div>

    
    
    

	<div>
		  
			
<div class="my_post_copyright clearfix">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article class="box-left">
    <img src="https://gitee.com/lilyn/pic/raw/master/logo.jpg">
  </article>
  <article class="box-right">
    <p><span>本文标题:</span><a href="/posts/453bf97e/">Canvas 学习（时钟 绘图板）</a></p>
    <p><span>文章作者:</span><a href="/" title="访问 LiLyn 的个人博客">LiLyn</a></p>
    <p><span>原始链接:</span><a href="/posts/453bf97e/" title="Canvas 学习（时钟 绘图板）">http://llmysnow.top/posts/453bf97e/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://llmysnow.top/posts/453bf97e/"  aria-label="复制成功！"></i></span>
  </p>
    <p><span>许可协议:</span>转载请保留原文链接及作者。</p>  
  </article>
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

		  
	</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"><i class="fa fa-tag"></i> JavaScript</a>
              <a href="/tags/Canvas/" rel="tag"><i class="fa fa-tag"></i> Canvas</a>
          </div>
		 
		<!--post-tags 彩色标签-->
		<script type="text/javascript">
		var tagsall=document.getElementsByClassName("post-tags")
		for (var i = tagsall.length - 1; i >= 0; i--){
			var tags=tagsall[i].getElementsByTagName("a");
			for (var j = tags.length - 1; j >= 0; j--) {
				var r=Math.floor(Math.random()*75+130);
				var g=Math.floor(Math.random()*75+100);
				var b=Math.floor(Math.random()*75+80);
				tags[j].style.background = "rgb("+r+","+g+","+b+")";
				tags[j].style.color = "#fff";
				tags[j].style.padding = "0 10px";
			}
		}                        
		</script>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/9cad4e3f/" rel="prev" title="JS 实现 sizeOf 函数，计算 object 占用了多少个 bytes">
      <i class="fa fa-chevron-left"></i> JS 实现 sizeOf 函数，计算 object 占用了多少个 bytes
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/2623edef/" rel="next" title="HTTP网络层性能优化">
      HTTP网络层性能优化 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#canvas%E7%94%BB%E5%9C%86"><span class="nav-text">canvas画圆</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Canvas"><span class="nav-text">Canvas</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%9C%E8%89%B2%E3%80%81%E6%A0%B7%E5%BC%8F%E5%92%8C%E9%98%B4%E5%BD%B1"><span class="nav-text">颜色、样式和阴影</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BA%BF%E6%9D%A1%E6%A0%B7%E5%BC%8F"><span class="nav-text">线条样式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9F%A9%E5%BD%A2"><span class="nav-text">矩形</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B7%AF%E5%BE%84"><span class="nav-text">路径</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AC%E6%8D%A2"><span class="nav-text">转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E6%9C%AC"><span class="nav-text">文本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BE%E5%83%8F%E7%BB%98%E5%88%B6"><span class="nav-text">图像绘制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%83%8F%E7%B4%A0%E6%93%8D%E4%BD%9C"><span class="nav-text">像素操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Canvas%E7%BB%83%E4%B9%A0"><span class="nav-text">Canvas练习</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%98%E5%88%B6%E5%9C%86%E5%BD%A2%E5%92%8C%E6%96%87%E6%9C%AC"><span class="nav-text">绘制圆形和文本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%98%E5%88%B6%E5%9B%BE%E5%BD%A2"><span class="nav-text">绘制图形</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%98%E5%88%B6%E6%97%B6%E9%92%9F"><span class="nav-text">绘制时钟</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Canvas-%E7%BB%98%E5%9B%BE%E6%9D%BF"><span class="nav-text">Canvas 绘图板</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-text">参考</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LiLyn"
      src="/images/logo.jpg">
  <p class="site-author-name" itemprop="name">LiLyn</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">162</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/llwodexue" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;llwodexue" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_38689395" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38689395" rel="noopener" target="_blank"><i class="fas fa-copyright fa-fw"></i>CSDN</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://scx69.gitee.io/blogs/" title="https:&#x2F;&#x2F;scx69.gitee.io&#x2F;blogs&#x2F;" rel="noopener" target="_blank">花开满城</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yixin-oss.gitee.io/daytoy/" title="http:&#x2F;&#x2F;yixin-oss.gitee.io&#x2F;daytoy&#x2F;" rel="noopener" target="_blank">daytoy</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiLyn</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">463k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:01</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>-->

<!--
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  <!-- 动态线条背景 -->
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  <!-- 页面点击小红心 -->
  <script type="text/javascript">
	!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
  </script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>